package io.qar.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qar.commons.mybatis.service.impl.CrudServiceImpl;
import io.qar.dao.BusFltRelationDao;
import io.qar.dto.BusFltRelationDTO;
import io.qar.entity.BusFltRelationEntity;
import io.qar.entity.OriFocT2001Entity;
import io.qar.service.BusFltRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 航班匹配表
 *
 * @author yyyyjinying 
 * @since 3.0 2023-10-26
 */
@Service
public class BusFltRelationServiceImpl extends CrudServiceImpl<BusFltRelationDao, BusFltRelationEntity, BusFltRelationDTO> implements BusFltRelationService {

    @Override
    public QueryWrapper<BusFltRelationEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<BusFltRelationEntity> wrapper = new QueryWrapper<>();
        return wrapper;
    }


    @Override
    @Async
    public boolean addAndupdateBatchById(List<Long> uuidlist, List<OriFocT2001Entity> lists){
        List<BusFltRelationEntity> busFltRelationLists = baseDao.selectList(null);
        List<Long> busFltRelationId = busFltRelationLists.stream().map(item -> item.getFltId()).collect(Collectors.toList());
        // 取差集用于新增
        List<Long> subtractList = (List<Long>) CollUtil.subtract(uuidlist, busFltRelationId);

        List<BusFltRelationEntity> addList = new ArrayList<>();
        List<BusFltRelationEntity> editList = new ArrayList<>();

        lists.stream().forEach(item -> {
            BusFltRelationEntity busFltRelationEntity = new BusFltRelationEntity();
            busFltRelationEntity.setFltId(item.getUuid());
            busFltRelationEntity.setCfpId(null);
            busFltRelationEntity.setQarId(null);
            busFltRelationEntity.setCrewId(null);
            // 新增 [2][2,3,4,5]
            if(subtractList.contains(item.getUuid())){
                addList.add(busFltRelationEntity);
            } else {
                editList.add(busFltRelationEntity);
            }
        });
        super.insertBatch(addList);
        super.updateBatchById(editList);
        return true;
    }
}